package com.itheima.controller;

import com.itheima.common.R;

import com.itheima.dto.OrderDto;
import com.itheima.entity.Orders;
import com.itheima.entity.Page;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired(required = false)
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        long userId = (long) session.getAttribute("user");
        ordersService.submit(orders,userId);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page,Integer pageSize,String number,String beginTime,String endTime,HttpSession session){
        Object user = session.getAttribute("user");
        Page<Orders> ordersPage=  ordersService.page(page,pageSize,number,beginTime,endTime);
      return  R.success(ordersPage);

    }

    @GetMapping("/userPage")
    public  R<Page<OrderDto>> userPage(Integer page,Integer pageSize,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        Page<OrderDto> ordersList= ordersService.userPage(page,pageSize,userId);
       return R.success(ordersList);
    }

    @PostMapping("again")
    public  R<OrderDto> again(@RequestBody OrderDto orderDto,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        orderDto.setUserId(userId);
    OrderDto   orderDto1 = ordersService.again(orderDto);


        return R.success(orderDto1);
    }
}
